/*--------------------------------------------------------------
 * File: devPublic.c 编码类型：C语言  UTF-8
 * Author WRS (1352509846@qq.com)
 * 功能: 模拟设备用的大公共信息
 * Version 0.1
 * Date 2024-11-06 
 * 
 * @copyright Copyright (c) 2024
 * 
----------------------------------------------------------------*/
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_log.h"
#include "devPublic.h"
#include "TypeDefine.h"
#include "data.h"
#include "devBML04.h"
#include "devBML12.h"
#include "devMD8.h"
#include "devMA8.h"
#include "devM26.h"
#include "devM38.h"
#include "myDebug.h"

// static const char *TAG = "devPublic";

Dev_t Dev_info= {0};

/*--------------------------------------------------------------
 * 功能: 初始化默认的一些设备函数指针，后续设备信息可以保存，在这个
 * 函数里对保存的信息进行恢复初始化
----------------------------------------------------------------*/
void dev_Public_Init(void)
{
    // 设备信息赋值
    // Dev_info.Dev_SCRx_type = simTypeML4;
    Dev_info.Dev_link_type = Lynkros_55FF;
    // Dev_info.Dev_init = Device1_init;
    // Dev_info.Dev_start = devBML04_Start;
    // Dev_info.Dev_stop = devBML04_Stop;
    Dev_info.Func_ReceiveVar = VARTable_RecVar_W;
    Dev_info.Func_ReceiveVarReturn = VARTable_RecVar_R;

    // 初始化BML04设备信息
    // 从NVS读取
    // bml04_info
    bml04_info_Init();
    bml12_info_Init();
    md8_info_Init();
    ma8_info_Init();
    m26_info_Init();
    m38_info_Init();
}


void VARTable_RecVar_W(uint32_t ctrl, uint8_t *pValue, void *pVarDes)
{
    struct LocalVarDescription_ST *pLocalVarDescription_st = (struct LocalVarDescription_ST *)pVarDes;
    uint8_t varDev_Type = 0;  // 当前收到的通讯帧里的变量的设备类型（-B发来的帧里都指定了设备类型，但是小章鱼发的帧设备类型是0）
    varDev_Type = pLocalVarDescription_st->VType_un.VType_st.Type;
    
    switch (varDev_Type)
    {
        case emTypeML4:
            VARTable_RecVar_BML04(ctrl, pValue, pVarDes);
            break;
        case emTypeML12:
            VARTable_RecVar_BML12(ctrl, pValue, pVarDes);
            break;
        case emTypeMD8:
            VARTable_RecVar_MD8(ctrl, pValue, pVarDes);
            break;
        case emTypeMA8:
            VARTable_RecVar_MA8(ctrl, pValue, pVarDes);
            break;
        case emTypeM26:
            VARTable_RecVar_M26(ctrl, pValue, pVarDes);
            break;
        case emTypeM38:
            VARTable_RecVar_M38(ctrl, pValue, pVarDes);
            break;
        
        default:
            MY_ERROR("varDev_Type = %d", varDev_Type);
            break;
    }

}

uint32_t VARTable_RecVar_R(uint32_t ctrl, uint8_t *pValue, void *pVarDes, uint8_t *devaddr)
{
    uint32_t ret = 0;
    struct LocalVarDescription_ST *pLocalVarDescription_st = (struct LocalVarDescription_ST *)pVarDes;
    uint8_t varDev_Type = 0;  // 当前收到的通讯帧里的变量的设备类型（-B发来的帧里都指定了设备类型，但是小章鱼发的帧设备类型是0）
    varDev_Type = pLocalVarDescription_st->VType_un.VType_st.Type;
    
    switch (varDev_Type)
    {
        case emTypeML4:
            ret = VARTable_RecVarR_BML04(ctrl, pValue, pVarDes, devaddr);
            break;
        case emTypeML12:
            ret = VARTable_RecVarR_BML12(ctrl, pValue, pVarDes, devaddr);
            break;
        case emTypeMD8:
            ret = VARTable_RecVarR_MD8(ctrl, pValue, pVarDes, devaddr);
            break;
        case emTypeMA8:
            ret = VARTable_RecVarR_MA8(ctrl, pValue, pVarDes, devaddr);
            break;
        case emTypeM26:
            ret = VARTable_RecVarR_M26(ctrl, pValue, pVarDes, devaddr);
            break;
        case emTypeM38:
            ret = VARTable_RecVarR_M38(ctrl, pValue, pVarDes, devaddr);
            break;
        
        default:
            MY_ERROR("varDev_Type = %d", varDev_Type);
            break;
    }
    return ret;
}
